package cn.al2.fast.lesson_2;

import java.util.concurrent.CompletableFuture;


public class FastFor {
    public static void main(String[] args) {
        //slow();
        fast();
    }


    public static void fast() {
        Job job = new Job();
        System.out.println("start");
        long start = System.currentTimeMillis();
        for (int i = 1; i <= 3; i++) {
            int finalI = i;
            CompletableFuture<String> f1 = CompletableFuture.supplyAsync(() -> {
                return job.one(finalI + "");
            }).whenComplete((r, e) -> {
                System.out.println(r);
            });
            CompletableFuture<String> f2 = CompletableFuture.supplyAsync(() -> {
                return job.two(finalI + "");
            }).whenComplete((r, e) -> {
                System.out.println(r);
            });
            CompletableFuture<String> f3 = CompletableFuture.supplyAsync(() -> {
                return job.three(finalI + "");
            }).whenComplete((r, e) -> {
                System.out.println(r);
            });
            CompletableFuture<Void> all = CompletableFuture.allOf(f1, f2, f3);
            all.join();
            System.out.println("三个都做完");
            // 数据库操作
        }
        System.out.println((start - System.currentTimeMillis()) + "耗时");
    }

    public static void slow() {
        Job job = new Job();
        System.out.println("start");
        long start = System.currentTimeMillis();
        for (int i = 1; i <= 3; i++) {
            System.out.println(job.one(i + ""));
            System.out.println(job.two(i + ""));
            System.out.println(job.three(i + ""));
        }
        System.out.println((start - System.currentTimeMillis()) + "耗时");
    }
}
